package com.yonyou.gmmc.service.customer.service;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.dms.dto.IfMessage;
import org.marketing.common.dms.dto.ResultCouponMessage;
import org.marketing.common.dms.dto.ResultCouponMessageDetail;
import org.marketing.common.dms.dto.ResultMessageDetail;
import org.marketing.common.dms.dto.ResultPointMessage;
import org.marketing.common.dms.dto.ResultPointMessageDetail;
import org.marketing.common.dms.http.HttpCommonUtil;
import org.marketing.common.dto.customerInfo.MemberDto;
import org.marketing.common.dto.dealerManager.DealerUsersDto;
import org.marketing.common.event.BizEvent;
import org.marketing.common.event.EventConsumeCouponDTO;
import org.marketing.common.event.EventPointsToPayDTO;
import org.marketing.common.event.enums.EventConstant;
import org.marketing.common.event.enums.EventConstant.EventBizStep;
import org.marketing.common.event.enums.EventConstant.EventBizType;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.DateUtil;
import org.marketing.common.util.MapConverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.icu.text.SimpleDateFormat;
import com.yonyou.gmmc.common.service.BaseService;
import com.yonyou.gmmc.common.sms.SmsHttpClient;
import com.yonyou.gmmc.service.customer.dao.TiDealerMapper;
import com.yonyou.gmmc.service.customer.dao.TiDealerUsersMapper;
import com.yonyou.gmmc.service.customer.dao.TmDealerMapper;
import com.yonyou.gmmc.service.customer.dao.TmPotentialUserMapper;
import com.yonyou.gmmc.service.customer.dao.TmUsersMapper;
import com.yonyou.gmmc.service.customer.dto.DealerSynchronousDto;
import com.yonyou.gmmc.service.customer.entity.ConsumeCouponPO;
import com.yonyou.gmmc.service.customer.entity.CouponPO;
import com.yonyou.gmmc.service.customer.entity.DeductPointPO;
import com.yonyou.gmmc.service.customer.entity.PointPO;
import com.yonyou.gmmc.service.customer.entity.TiDealer;
import com.yonyou.gmmc.service.customer.entity.TmDealer;
import com.yonyou.gmmc.service.customer.entity.TmUsers;
@Transactional
@Service("dcmscustomerService")
public class DCMSCustomerService {
	private Logger                   logger = Logger.getLogger(CustomerService.class);

    @Autowired
    private TmPotentialUserMapper    tmPotentialUserMapper;

    @Autowired
    private BaseService              eventService;

    @Autowired
    private TmDealerMapper           tmDealerMapper;

    @Autowired
    private TiDealerMapper           tiDealerMapper;

    @Autowired
    private TiDealerUsersMapper      tiDealerUsersMapper;

    @Autowired
    private TmUsersMapper            tmUsersMapper;

    @Value("${DCMS-url}")
    private String                   dcmsUrl;
    @Value("${SMS-pass}")
    private String                   smsPass;
    @Value("${SMS-key}")
    private String                   smsKey;
    @Value("${SMS_CDKEY}")
    private String                   SMS_CDKEY;
    @Value("${SMS_PASS}")
    private String                   SMS_PASS;
    @Value("${SMS_BASEURL}")
    private String                   SMS_BASEURL;
    @Value("${SMS_TEMPLATEID_SC}")
    private String                   SMS_TEMPLATEID_SC;

    @Value("${DEALER-URL}")
    private String                   dealerUrl;

    @Value("${DEALER_USERS-URL}")
    private String                   dealerUsersUrl;
    /**
     * 查询用户积分（DCMS调用）
     * 
     * @param info
     * @return
     */
    public ResultPointMessage getPointByMobile(IfMessage<List<PointPO>> info) {
        ResultPointMessage message = new ResultPointMessage();
        message.setIfCode(info.getIfCode());
        message.setTransferNo(info.getTransferNo());
        // 判断ifCode参数
        if (!info.getIfCode().toString().equals("IC011") && info.getIfCode().toString() != "IC011") {
            // ifCode不正确，返回错误信息
            logger.info("请求参数ifCode有误:");
            message.setResult("400");
            List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
            ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
            resultMessageDetail.setMsg("ifCode参数有误！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else {
            // 获得电话号码
            String mobile = info.getIfData().get(0).getMobile();
            logger.info("接收的电话:" + mobile);
            // 通过电话查询用户积分，姓名
            Map<String, Object> pointMap = tmPotentialUserMapper.getPointsByMobile(mobile);
            // 判断查询结果是否为空
            if (pointMap == null || pointMap.size() == 0) {
                logger.info("通过手机查询用户不存在");
                message.setResult("400");
                List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
                ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
                resultMessageDetail.setMsg("用户不存在！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else {
                message.setResult("200");
                List<ResultPointMessageDetail> list = new ArrayList<ResultPointMessageDetail>();
                ResultPointMessageDetail RPMessageDetail = new ResultPointMessageDetail();
                // 扫描 1151
                // logger.info("通过手机查询用户积分不为空，用户姓名：" + pointMap.get("userName") + " 用户积分：" + pointMap.get("pointsNum"));
                // 判断名字是否为空
                if (pointMap.get("userName") != null) {
                    RPMessageDetail.setName(pointMap.get("userName").toString());
                } else {
                    RPMessageDetail.setName("");
                }
                // 判断积分是否为空
                if (pointMap.get("pointsNum") != null) {
                    // logger.info("通过手机查询用户积分不为空，用户姓名：" + pointMap.get("userName") + " 用户积分："
                    // + pointMap.get("pointsNum"));
                    RPMessageDetail.setPoints(Integer.valueOf(pointMap.get("pointsNum").toString()));
                } else {
                    // 积分为空时传入0分
                    // logger.info("通过手机查询用户积分为空，用户姓名：" + pointMap.get("userName"));
                    RPMessageDetail.setPoints(0);
                }
                list.add(RPMessageDetail);
                message.setResultList(list);
            }
        }
        return message;
    }
    
    /**
     * 用户扣减积分（DCMS调用）
     * 
     * @param info
     * @return
     */
    public ResultPointMessage checkDeductPoint(IfMessage<List<DeductPointPO>> info) {
        ResultPointMessage message = new ResultPointMessage();
        // 获取接收积分
        Integer point = info.getIfData().get(0).getPoints();
        logger.info("请求参数获得的积分:" + point);
        // 获取验证码
        String code = info.getIfData().get(0).getCode();
        // logger.info("请求参数获得的验证码:" + code);
        if (point > 0 && (code == null || code == "")) {
            // 发送验证码
            message = updateCodeByMobile(info);
        } else if (point > 0 && code != null && code != "") {
            // 扣分
            message = deductPointByMobile(info);
        } else if (point < 0 && (code == null || code == "")) {
            // 返还积分
            message = returnDeductPoint(info);
        }
        return message;
    }
    
    /**
     * 更新验证码（DCMS调用）
     * 
     * @param info
     * @return
     */
    public ResultPointMessage updateCodeByMobile(IfMessage<List<DeductPointPO>> info) {
        ResultPointMessage message = new ResultPointMessage();
        message.setIfCode(info.getIfCode());
        message.setTransferNo(info.getTransferNo());
        message.setResult("400");
        // 获得接收的积分
        Integer point = info.getIfData().get(0).getPoints();
        logger.info("请求获得的积分:" + point);
        String afterSalesCode = info.getIfData().get(0).getAfterSalesCode();
        // logger.info("请求获得的经销商代码:" + afterSalesCode);
        // 查询经销商名称
        String delearName = tmPotentialUserMapper.getDelearName(afterSalesCode);
        // logger.info("查询获得的经销商名称:" + delearName);
        List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
        ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
        // 判断ifCode参数是否正确
        if (!info.getIfCode().toString().equals("IC012") && info.getIfCode().toString() != "IC012") {
            // logger.info("请求参数ifCode有误:" + info.getIfCode());
            resultMessageDetail.setMsg("ifCode参数有误！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else if (delearName == null || delearName == "") {
            // 判断经销商名称
            // logger.info("经销商代码无效:" + afterSalesCode);
            resultMessageDetail.setMsg("经销商代码无效！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else {
            String mobile = info.getIfData().get(0).getMobile();
            // 通过手机查询用户积分信息
            Map<String, Object> pointMap = tmPotentialUserMapper.getPointsByMobile(mobile);
            // 判断用户是否存在
            if (pointMap == null || pointMap.size() == 0) {
                logger.info("通过手机查询用户不存在");
                resultMessageDetail.setMsg("用户不存在！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else {
                Integer pointNum = 0;
                if (pointMap.get("pointsNum") != null) {
                    pointNum = Integer.valueOf(pointMap.get("pointsNum").toString());
                }
                // 判断积分是否充足
                if (point > pointNum) {
                    logger.info("积分不足！");
                    resultMessageDetail.setMsg("积分不足！");
                    resultErrList.add(resultMessageDetail);
                    message.setResultErrList(resultErrList);
                } else {
                    message.setResult("200");
                    // 获得openId
                    String openId = pointMap.get("openId").toString();
                    String userName = "";
                    // 判断姓名是否为空
                    if (pointMap.get("userName") != null) {
                        userName = pointMap.get("userName").toString();
                    }
                    logger.info("openId：" + openId);
                    logger.info("用户姓名：" + userName);
                    logger.info("扣减积分：" + point);
                    MemberDto memberDto = new MemberDto();
                    memberDto.setOpenId(openId);
                    String Num = "";
                    // 查询该用户是否存在符合要就的验证码
                    List<MemberDto> list = tmPotentialUserMapper.queryPointIdenCode(openId);
                    if (list != null && list.size() > 0) {
                        Num = list.get(0).getPointIdenCode();
                    } else {
                        // 生成验证码
                        for (int i = 0; i < 6; i++) {

                            // 代码扫描 1612
                            String a = String.valueOf(new SecureRandom().nextInt());
                            Num += a.charAt(6);
                        }
                        memberDto.setPointIdenCode(Num);
                        // 更新验证码
                        tmPotentialUserMapper.updatePointIdenCode(memberDto);
                    }
                    // logger.info("验证码：" + Num);
                    // logger.info("发送短信To：" + mobile);
                    // 调用短信接口
                    String s = SmsHttpClient.sendSms(mobile,
                                                     "尊敬的 " + userName + " 先生/女士，您将在" + delearName + "销售服务店，消费积分："
                                                             + point + " 分 ，您的验证码为：" + Num + "(5分钟内有效)",
                                                     smsPass, smsKey, SMS_CDKEY, SMS_PASS, SMS_BASEURL);
                    logger.info("发送短信————End");
                    logger.info("返回码————" + s);
                }
            }
        }
        return message;
    }
    
    /**
     * 扣减积分（DCMS调用）
     * 
     * @param info
     * @return
     */
    public ResultPointMessage deductPointByMobile(IfMessage<List<DeductPointPO>> info) {
        ResultPointMessage message = new ResultPointMessage();
        message.setIfCode(info.getIfCode());
        message.setTransferNo(info.getTransferNo());
        message.setResult("400");
        // 获取接收积分
        Integer point = info.getIfData().get(0).getPoints();
        logger.info("请求获得的积分:" + point);
        // 获取工单号
        String workOrderNo = info.getIfData().get(0).getWorkOrderNo();
        // logger.info("请求获得的工单号:" + workOrderNo);
        // 获取经销商代码
        String afterSalesCode = info.getIfData().get(0).getAfterSalesCode();
        // logger.info("请求获得的经销商代码:" + afterSalesCode);
        // 查询经销商名称
        String delearName = tmPotentialUserMapper.getDelearName(afterSalesCode);
        // logger.info("查询获得的经销商名称:" + delearName);
        // 获取接收验证码
        String pointIdenCode = info.getIfData().get(0).getCode();
        // logger.info("请求获得的验证码:" + pointIdenCode);
        List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
        ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
        // 判断请求参数ifCode是否正确
        if (!info.getIfCode().toString().equals("IC012") && info.getIfCode().toString() != "IC012") {
            logger.info("请求参数ifCode有误:");
            resultMessageDetail.setMsg("ifCode参数有误！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else if (delearName == null || delearName == "") {
            // 判断经销商名称
            // logger.info("经销商代码无效:" + afterSalesCode);
            resultMessageDetail.setMsg("经销商代码无效！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else if (pointIdenCode == null || pointIdenCode == "") {
            // 判断验证码格式
            logger.info("验证码不能为空:");
            resultMessageDetail.setMsg("验证码不能为空！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else {
            // 获得接收的电话
            String mobile = info.getIfData().get(0).getMobile();
            // 通过电话查询用户积分信息
            Map<String, Object> pointMap = tmPotentialUserMapper.getPointsByMobile(mobile);
            // 判断用户是否存在
            if (pointMap == null || pointMap.size() == 0) {
                logger.info("通过手机查询用户不存在");
                resultMessageDetail.setMsg("用户不存在！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else {
                // 获取openId
                String openId = pointMap.get("openId").toString();
                // 对比页面输入的验证码
                List<MemberDto> memberDto = tmPotentialUserMapper.queryPointPotential(openId);
                for (MemberDto memberList : memberDto) {
                    // 判断验证码是否正确
                    if (pointIdenCode.equals(memberList.getPointIdenCode())) {
                        Date nowDate = new Date();
                        // 计算时间差(秒)
                        Long diffTime = (nowDate.getTime() - memberList.getPointIdenDate().getTime()) / 1000;
                        if (diffTime <= 300) {
                            Integer pointNum = 0;
                            if (pointMap.get("pointsNum") != null) {
                                pointNum = Integer.valueOf(pointMap.get("pointsNum").toString());
                            }
                            // 判断积分是否充足
                            if (point > pointNum) {
                                logger.info("积分不足！");
                                resultMessageDetail.setMsg("积分不足！");
                                resultErrList.add(resultMessageDetail);
                                message.setResultErrList(resultErrList);
                            } else {
                                try {
                                    // 保存经销商名称和经销商代码
                                    // String dealerNameCode=delearName+"（"+afterSalesCode+"）";
                                    // 2017-06-23 修改显示的原因，不要代码，只要经销商名称
                                    String dealerNameCode = delearName;
                                    // 发送扣分成功事件
                                    logger.info("DCMS扣分提交结果事件————Start");
                                    EventPointsToPayDTO pointsEventDto = new EventPointsToPayDTO();
                                    pointsEventDto.setPhone(mobile);
                                    // logger.info("扣分的手机号——" + mobile);
                                    pointsEventDto.setPointsNum(point.longValue());
                                    // logger.info("扣减的积分——" + pointsEventDto.getPointsNum());
                                    pointsEventDto.setWorkOrderNo(workOrderNo);
                                    // logger.info("工单号——" + pointsEventDto.getWorkOrderNo());
                                    pointsEventDto.setUserId(Integer.valueOf(pointMap.get("userId").toString()));
                                    // logger.info("扣分用户id——" + pointsEventDto.getUserId());
                                    pointsEventDto.setDealerName(dealerNameCode);
                                    logger.info("扣分用户id——" + pointsEventDto.getDealerName());
                                    BizEvent event = new BizEvent();
                                    event.setEventBizType(EventBizType.COMMON);
                                    event.setEventBizStep(EventBizStep.COMMON_POINTS_DEDUCT);
                                    event.setEventKey(pointsEventDto.getWorkOrderNo() + memberList.getDealerCode());
                                    event.setEventData(MapConverUtil.PO2Map(pointsEventDto));
                                    // eventService.sendEvent2MQ(event);
                                    String x = eventService.sendEvent2MQ(event);
                                    logger.info("MQ返回结果>>>>>>>>>>>>>>>>>>>>" + x);
                                    logger.info("用户扣分事件————End");

                                    // 清空验证码
                                    tmPotentialUserMapper.emptyPointIdenCode(openId);
                                    logger.info("IC012扣减积分验证码清空完成");
                                    message.setResult("200");
                                } catch (Exception e) {
                                    logger.info("Exception:", e);
                                }
                            }
                        } else {
                            logger.info("验证码超时");
                            resultMessageDetail.setMsg("验证码超时！");
                            resultErrList.add(resultMessageDetail);
                            message.setResultErrList(resultErrList);
                        }
                    } else {
                        logger.info("验证码不正确");
                        resultMessageDetail.setMsg("验证码不正确！");
                        resultErrList.add(resultMessageDetail);
                        message.setResultErrList(resultErrList);
                    }
                }
            }
        }
        return message;
    }

    /**
     * 返还扣减积分（DCMS调用）
     * 
     * @param info
     * @return
     */
    public ResultPointMessage returnDeductPoint(IfMessage<List<DeductPointPO>> info) {
        ResultPointMessage message = new ResultPointMessage();
        message.setIfCode(info.getIfCode());
        message.setTransferNo(info.getTransferNo());
        message.setResult("400");
        // 获取接收积分
        Integer point = info.getIfData().get(0).getPoints();
        logger.info("请求参数获得的积分:" + point);
        // 获取经销商代码
        String afterSalesCode = info.getIfData().get(0).getAfterSalesCode();
        // logger.info("请求获得的经销商代码:" + afterSalesCode);
        // 查询经销商名称
        String delearName = tmPotentialUserMapper.getDelearName(afterSalesCode);
        logger.info("查询获得的经销商名称:" + delearName);
        // 获取工单号
        String workOrderNo = info.getIfData().get(0).getWorkOrderNo();
        // logger.info("请求参数获得的工单号:" + workOrderNo);
        List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
        ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
        // 判断请求参数ifCode是否正确
        if (!info.getIfCode().toString().equals("IC012") && info.getIfCode().toString() != "IC012") {
            logger.info("请求参数ifCode有误:" + info.getIfCode());
            resultMessageDetail.setMsg("ifCode参数有误！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else if (delearName == null || delearName == "") {
            // 判断经销商名称
            // logger.info("经销商代码无效:" + afterSalesCode);
            resultMessageDetail.setMsg("经销商代码无效！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else {
            // 获得接收的电话
            String mobile = info.getIfData().get(0).getMobile();
            // 通过电话查询用户积分信息
            Map<String, Object> pointMap = tmPotentialUserMapper.getPointsByMobile(mobile);
            // 判断用户是否存在
            if (pointMap == null || pointMap.size() == 0) {
                logger.info("通过手机查询用户不存在");
                resultMessageDetail.setMsg("用户不存在！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else {
                // 获取openId
                Integer userId = Integer.valueOf(pointMap.get("userId").toString());
                Integer checkPoint = point;
                Integer checkLog = tmPotentialUserMapper.checkLog(userId, checkPoint, workOrderNo);
                if (checkLog == 0) {
                    logger.info("根据工单号没有查到积分记录");
                    resultMessageDetail.setMsg("根据工单号没有查到积分记录！");
                    resultErrList.add(resultMessageDetail);
                    message.setResultErrList(resultErrList);
                } else {
                    try {
                        // 保存经销商名称和经销商代码
                        // String dealerNameCode=delearName+"（"+afterSalesCode+"）";
                        // 2017-06-23 修改显示的原因，不要代码，只要经销商名称
                        String dealerNameCode = delearName;
                        String signWON = workOrderNo;
                        for (int i = 0; i < 6; i++) {
                            // 代码扫描 1612
                            String a = String.valueOf(new SecureRandom().nextInt());
                            signWON += a.charAt(6);
                        }
                        logger.info("返还积分");
                        // 发送返还积分成功事件
                        logger.info("DCMS扣分提交结果事件————Start");
                        EventPointsToPayDTO pointsEventDto = new EventPointsToPayDTO();
                        pointsEventDto.setPhone(mobile);
                        // logger.info("扣分的手机号——" + mobile);
                        pointsEventDto.setPointsNum(point.longValue());
                        logger.info("返还的积分——" + pointsEventDto.getPointsNum());
                        pointsEventDto.setWorkOrderNo(workOrderNo);
                        // logger.info("工单号——" + pointsEventDto.getWorkOrderNo());
                        pointsEventDto.setUserId(Integer.valueOf(pointMap.get("userId").toString()));
                        // logger.info("返还积分用户id——" + pointsEventDto.getUserId());
                        pointsEventDto.setDealerName(dealerNameCode);
                        // logger.info("返还积分用户id——" + pointsEventDto.getDealerName());
                        BizEvent event = new BizEvent();
                        event.setEventBizType(EventBizType.COMMON);
                        event.setEventBizStep(EventBizStep.COMMON_POINTS_RETURN);
                        event.setEventKey(signWON);
                        event.setEventData(MapConverUtil.PO2Map(pointsEventDto));
                        // eventService.sendEvent2MQ(event);
                        String x = eventService.sendEvent2MQ(event);
                        logger.info("MQ返回结果>>>>>>>>>>>>>>>>>>>>" + x);
                        logger.info("用户返还积分事件————End");
                        message.setResult("200");
                    } catch (Exception e) {
                        logger.info("Exception:", e);
                    }

                }
            }
        }
        return message;
    }
    /**
     * 查询有效优惠券（IC015）
     * 
     * @param info
     * @return
     */
    public ResultCouponMessage queryCoupon(IfMessage<List<CouponPO>> info) {
    	logger.info("查询有效优惠卷开始");
        ResultCouponMessage message = new ResultCouponMessage();
        message.setIfCode(info.getIfCode());
        message.setTransferNo(info.getTransferNo());
        message.setResult("400");
        List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
        ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
        // 获得电话号码
        String mobile = info.getIfData().get(0).getMobile();
        logger.info("接收的电话:" + mobile);
        String afterSalesCode = info.getIfData().get(0).getAfterSalesCode();
        // logger.info("接收的售后代码:" + afterSalesCode);
        // 判断ifCode参数
        if (!info.getIfCode().toString().equals("IC015") && info.getIfCode().toString() != "IC015") {
            // ifCode不正确，返回错误信息
            // logger.info("请求参数ifCode有误:" + info.getIfCode());
            resultMessageDetail.setMsg("ifCode参数有误！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else if (mobile == null || mobile == "") {
            // 判断手机号码
            // logger.info("手机号码不能为空:" + mobile);
            resultMessageDetail.setMsg("手机号码不能为空！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else if (afterSalesCode == null || afterSalesCode == "") {
            // 判断经销商代码
            // logger.info("经销商代码不能为空:" + mobile);
            resultMessageDetail.setMsg("经销商代码不能为空！");
            resultErrList.add(resultMessageDetail);
            message.setResultErrList(resultErrList);
        } else {
            List<ResultCouponMessageDetail> couponList = tmPotentialUserMapper.queryCoupon(mobile, afterSalesCode);
            // logger.info("通过电话查询到的有效优惠卷：" + couponList);
            message.setResult("200");
            message.setResultList(couponList);
            logger.info("通过电话查询有效优惠完成！");
        }
        return message;
    }
    /**
     * 判断优惠券核销（IC016）
     * 
     * @param info
     * @return
     */
    public ResultCouponMessage checkConsumeCoupon(IfMessage<List<ConsumeCouponPO>> info) {
    	ResultCouponMessage message = new ResultCouponMessage();
        String code = info.getIfData().get(0).getCode();
        // logger.info("获取的验证码为：" + code);
        Integer status = info.getIfData().get(0).getStatus();
        // logger.info("获取的优惠卷状态为：" + status);
        if (status == 0 && (code == null || code == "")) {
            logger.info("发送验证码");
            message = sendCouponCode(info);
        } else if (status == 0 && code != null && code != "") {
            logger.info("核销优惠卷");
            message = consumeCoupon(info);
        } else if (status == 1) {
            logger.info("取消工单");
            message = cancelCoupon(info);
        }
        return message;
    }
    /**
     * 优惠券核销发送验证码（IC016）
     * 
     * @param info
     * @return
     */
    private ResultCouponMessage sendCouponCode(IfMessage<List<ConsumeCouponPO>> info) {
        try {
            ResultCouponMessage message = new ResultCouponMessage();
            message.setIfCode(info.getIfCode());
            message.setTransferNo(info.getTransferNo());
            message.setResult("400");
            List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
            ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
            String afterSalesCode = info.getIfData().get(0).getAfterSalesCode();
            // logger.info("请求获得的经销商代码:" + afterSalesCode);
            // 查询经销商名称
            String delearName = tmPotentialUserMapper.getDelearName(afterSalesCode);
            // logger.info("查询获得的经销商名称:" + delearName);
            String mobile = info.getIfData().get(0).getMobile();
            logger.info("请求获得的手机号码:" + mobile);
            Long cardNo = info.getIfData().get(0).getCardNo();
            logger.info("请求获得的卡卷号:" + cardNo);
            // 判断ifCode参数是否正确
            if (!info.getIfCode().toString().equals("IC016") && info.getIfCode().toString() != "IC016") {
                // logger.info("请求参数ifCode有误:" + info.getIfCode());
                resultMessageDetail.setMsg("ifCode参数有误！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (delearName == null || delearName == "") {
                // 判断经销商名称
                // logger.info("经销商代码无效:" + afterSalesCode);
                logger.info("经销商代码无效:");
                resultMessageDetail.setMsg("经销商代码无效！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (mobile == null || mobile == "") {
                // 判断手机号码
                // logger.info("手机号码不能为空:" + mobile);
                logger.info("手机号码不能为空:");
                resultMessageDetail.setMsg("手机号码不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (cardNo == null) {
                // 判断卷号
                logger.info("卷号不能为空:");
                resultMessageDetail.setMsg("卷号不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (afterSalesCode == null || afterSalesCode == "") {
                // 判断经销商代码
                // logger.info("经销商代码不能为空:" + mobile);
                logger.info("经销商代码不能为空:");
                resultMessageDetail.setMsg("经销商代码不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else {
                Map<String, Object> couponMap = tmPotentialUserMapper.queryExistCoupon(cardNo, mobile, 1);
                if (couponMap == null) {
                    logger.info("没有查询到有效的优惠卷");
                    resultMessageDetail.setMsg("没有查询到有效的优惠卷！");
                    resultErrList.add(resultMessageDetail);
                    message.setResultErrList(resultErrList);
                } else {
                    // 获得openId
                    String openId = couponMap.get("openId").toString();
                    String userName = "";
                    // 判断姓名是否为空
                    if (couponMap.get("userName") != null) {
                        userName = couponMap.get("userName").toString();
                    }
                    // logger.info("openId：" + openId);
                    // logger.info("用户姓名：" + userName);
                    MemberDto memberDto = new MemberDto();
                    memberDto.setOpenId(openId);
                    String Num = "";
                    // 查询该用户是否存在符合要就的验证码
                    List<MemberDto> list = tmPotentialUserMapper.queryCouponIdenCode(openId);
                    if (list != null && list.size() > 0) {
                        Num = list.get(0).getCouponIdenCode();
                    } else {
                        // 生成验证码
                        for (int i = 0; i < 6; i++) {
                            // 代码扫描 1612
                            String a = String.valueOf(new SecureRandom().nextInt());
                            Num += a.charAt(6);
                        }
                        memberDto.setCouponIdenCode(Num);
                        // 更新验证码
                        tmPotentialUserMapper.updateCouponIdenCode(memberDto);
                    }
                    logger.info("验证码：" + Num);
                    logger.info("发送短信To：" + mobile);
                    // 调用短信接口
                    String s = SmsHttpClient.sendSms(mobile,
                                                     "尊敬的 " + userName + " 先生/女士，您将在" + delearName
                                                             + "销售服务店，核销优惠卷 ，您的验证码为：" + Num + "(5分钟内有效)",
                                                     smsPass, smsKey, SMS_CDKEY, SMS_PASS, SMS_BASEURL);
                    logger.info("发送短信————End");
                    logger.info("返回码————" + s);
                    message.setResult("200");
                }
            }
            return message;
        } catch (Exception e) {
            logger.error("异常：" + e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }
    }
    /**
     * 核销优惠卷（IC016）
     * 
     * @param info
     * @return
     */
    private ResultCouponMessage consumeCoupon(IfMessage<List<ConsumeCouponPO>> info) {
        try {
            ResultCouponMessage message = new ResultCouponMessage();
            message.setIfCode(info.getIfCode());
            message.setTransferNo(info.getTransferNo());
            message.setResult("400");
            // 获取工单号
            String workOrderNo = info.getIfData().get(0).getWorkOrderNo();
            // logger.info("请求获得的工单号:" + workOrderNo);
            // 获取经销商代码
            String afterSalesCode = info.getIfData().get(0).getAfterSalesCode();
            // logger.info("请求获得的经销商代码:" + afterSalesCode);
            // 查询经销商名称
            String delearName = tmPotentialUserMapper.getDelearName(afterSalesCode);
            // logger.info("查询获得的经销商名称:" + delearName);
            // 获取接收验证码
            String couponIdenCode = info.getIfData().get(0).getCode();
            // logger.info("请求获得的验证码:" + couponIdenCode);
            Long cardNo = info.getIfData().get(0).getCardNo();
            // logger.info("请求获得的卡卷号:" + cardNo);
            // 获得接收的电话
            String mobile = info.getIfData().get(0).getMobile();
            logger.info("请求获得的电话:" + mobile);
            List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
            ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
            // 判断请求参数ifCode是否正确
            if (!info.getIfCode().toString().equals("IC016") && info.getIfCode().toString() != "IC016") {
                // logger.info("请求参数ifCode有误:" + info.getIfCode());
                resultMessageDetail.setMsg("ifCode参数有误！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (cardNo == null) {
                // 判断卷号
                logger.info("卷号不能为空：");
                resultMessageDetail.setMsg("卷号不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (mobile == null || mobile == "") {
                // 判断手机号码
                logger.info("手机号码不能为空：" + mobile);
                resultMessageDetail.setMsg("手机号码不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (workOrderNo == null || workOrderNo == "") {
                // 判断工单号
                // logger.info("工单号不能为空：" + workOrderNo);
                resultMessageDetail.setMsg("工单号不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (afterSalesCode == null || afterSalesCode == "") {
                // 判断经销商代码
                logger.info("经销商代码不能为空：");
                resultMessageDetail.setMsg("经销商代码不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (delearName == null || delearName == "") {
                // 判断经销商名称
                // logger.info("经销商代码无效:" + afterSalesCode);
                resultMessageDetail.setMsg("经销商代码无效！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (couponIdenCode == null || couponIdenCode == "") {
                // 判断验证码格式
                logger.info("验证码不能为空:" + couponIdenCode);
                resultMessageDetail.setMsg("验证码不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else {
                Map<String, Object> couponMap = tmPotentialUserMapper.queryExistCoupon(cardNo, mobile, 1);
                if (couponMap == null) {
                    // 判断是否存在有效优惠卷
                    logger.info("没有查询到有效优惠卷！");
                    resultMessageDetail.setMsg("没有查询到有效优惠卷！");
                    resultErrList.add(resultMessageDetail);
                    message.setResultErrList(resultErrList);
                } else {
                    // 获取openId
                    String openId = couponMap.get("openId").toString();
                    Integer potentialUserId = Integer.valueOf(couponMap.get("potentialUserId").toString());
                    // 对比页面输入的验证码
                    List<MemberDto> memberDto = tmPotentialUserMapper.queryCouponPotential(openId);
                    for (MemberDto memberList : memberDto) {
                        // 判断验证码是否正确
                        // logger.info("请求获得的验证码：" + couponIdenCode + "==查询的验证码：" + memberList.getCouponIdenCode());
                        if (couponIdenCode.equals(memberList.getCouponIdenCode())) {
                            Date nowDate = new Date();
                            // 计算时间差(秒)
                            Long diffTime = (nowDate.getTime() - memberList.getCouponIdenDate().getTime()) / 1000;
                            if (diffTime <= 300) {
                                // 执行操作
                                String couponRemarks = "DCMS核销优惠卷";
                                tmPotentialUserMapper.consumeCoupon(cardNo, workOrderNo, afterSalesCode,
                                                                    potentialUserId, couponRemarks);
                                tmPotentialUserMapper.emptyCouponIdenDate(potentialUserId);
                                logger.info("IC016核销优惠券清空验证码完成");
                                message.setResult("200");
                                logger.info("DCMS核销优惠卷完成！");
                                // 推送消息模板
                                logger.info("推送卡券核销消息模板——Start");
                                Map<String, Object> queryMap = new HashMap<String, Object>();
                                String dealerName = tmPotentialUserMapper.selectDealerName(afterSalesCode);
                                logger.info("经销商名字为>>>>>>" + dealerName);
                                String ticketName = tmPotentialUserMapper.selectTicketName(cardNo);
                                logger.info("优惠券名为>>>>>>" + ticketName);
                                queryMap.put("CONSUMEDATE", DateUtil.formateDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
                                queryMap.put("OPENID", openId);
                                queryMap.put("DEALERNAME", dealerName);
                                queryMap.put("TICKETID", cardNo);
                                queryMap.put("TICKETNAME", ticketName);
                                SendTicketConsume(queryMap);
                                logger.info("推送卡券核销消息模板——End");
                            } else {
                                logger.info("验证码超时");
                                resultMessageDetail.setMsg("验证码超时！");
                                resultErrList.add(resultMessageDetail);
                                message.setResultErrList(resultErrList);
                            }
                        } else {
                            logger.info("验证码不正确");
                            resultMessageDetail.setMsg("验证码不正确！");
                            resultErrList.add(resultMessageDetail);
                            message.setResultErrList(resultErrList);
                        }
                    }
                }
            }
            return message;

        } catch (Exception e) {
            logger.error("异常：" + e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }
    }
    /**
     * 取消优惠卷工单（IC016）
     * 
     * @param info
     * @return
     */
    private ResultCouponMessage cancelCoupon(IfMessage<List<ConsumeCouponPO>> info) {
        try {
            ResultCouponMessage message = new ResultCouponMessage();
            message.setIfCode(info.getIfCode());
            message.setTransferNo(info.getTransferNo());
            message.setResult("400");
            // 获取工单号
            String workOrderNo = info.getIfData().get(0).getWorkOrderNo();
            // logger.info("请求获得的工单号:" + workOrderNo);
            // 获取经销商代码
            String afterSalesCode = info.getIfData().get(0).getAfterSalesCode();
            // logger.info("请求获得的经销商代码:" + afterSalesCode);
            // 查询经销商名称
            String delearName = tmPotentialUserMapper.getDelearName(afterSalesCode);
            // logger.info("查询获得的经销商名称:" + delearName);
            // 获取接收验证码
            Long cardNo = info.getIfData().get(0).getCardNo();
            logger.info("请求获得的卡卷号:" + cardNo);
            // 获得接收的电话
            String mobile = info.getIfData().get(0).getMobile();
            logger.info("请求获得的电话:" + mobile);
            List<ResultMessageDetail> resultErrList = new ArrayList<ResultMessageDetail>();
            ResultMessageDetail resultMessageDetail = new ResultMessageDetail();
            // 判断请求参数ifCode是否正确
            if (!info.getIfCode().toString().equals("IC016") && info.getIfCode().toString() != "IC016") {
                // logger.info("请求参数ifCode有误:" + info.getIfCode());
                resultMessageDetail.setMsg("ifCode参数有误！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (cardNo == null) {
                // 判断卷号
                logger.info("卷号不能为空：");
                resultMessageDetail.setMsg("卷号不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (mobile == null || mobile == "") {
                // 判断手机号码
                logger.info("手机号码不能为空：");
                resultMessageDetail.setMsg("手机号码不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (workOrderNo == null || workOrderNo == "") {
                // 判断工单号
                logger.info("工单号不能为空：" + workOrderNo);
                resultMessageDetail.setMsg("工单号不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (afterSalesCode == null || afterSalesCode == "") {
                // 判断经销商代码
                logger.info("经销商代码不能为空：");
                resultMessageDetail.setMsg("经销商代码不能为空！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else if (delearName == null || delearName == "") {
                // 判断经销商名称
                // logger.info("经销商代码无效:" + afterSalesCode);
                resultMessageDetail.setMsg("经销商代码无效！");
                resultErrList.add(resultMessageDetail);
                message.setResultErrList(resultErrList);
            } else {
                Integer checkLog = tmPotentialUserMapper.checkCouponLog(info.getIfData().get(0));
                if (checkLog == 0) {
                    logger.info("根据工单号没有查到已核销优惠卷记录");
                    resultMessageDetail.setMsg("根据工单号没有查到已核销优惠卷记录！");
                    resultErrList.add(resultMessageDetail);
                    message.setResultErrList(resultErrList);
                } else {
                    // 取消优惠卷操作
                    Map<String, Object> couponMap = tmPotentialUserMapper.queryExistCoupon(cardNo, mobile, 2);
                    if (couponMap == null) {
                        // 判断是否存在已核销优惠卷
                        logger.info("没有查询到已核销优惠卷！");
                        resultMessageDetail.setMsg("没有查询到已核销优惠卷！");
                        resultErrList.add(resultMessageDetail);
                        message.setResultErrList(resultErrList);
                    } else {
                        Integer potentialUserId = Integer.valueOf(couponMap.get("potentialUserId").toString());
                        String couponRemarks = "DCMS取消优惠卷核销";
                        tmPotentialUserMapper.cancelConsumeCoupon(cardNo, potentialUserId, couponRemarks);
                        message.setResult("200");
                        logger.info("DCMS取消优惠卷核销完成！");
                    }
                }
            }
            return message;
        } catch (Exception e) {
            logger.error("异常：" + e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }
    }
    public void SendTicketConsume(Map<String, Object> map) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date ConsumeDate = sdf.parse(map.get("CONSUMEDATE").toString());
            logger.info("推送卡券核销消息模板事件到MQ");
            // 具体业务参数
            EventConsumeCouponDTO eventDate = new EventConsumeCouponDTO();
            eventDate.setOpenId(map.get("OPENID").toString());
            eventDate.setConsumeDate(ConsumeDate);
            eventDate.setDealerName(map.get("DEALERNAME").toString());
            eventDate.setTicketId(map.get("TICKETID").toString());
            eventDate.setTicketName(map.get("TICKETNAME").toString());
            // 业务事件
            BizEvent bizEvent = new BizEvent();
            bizEvent.setEventKey("TICKET_CONSUME_TEMPLATE:" + new Date().getTime());
            bizEvent.setEventBizType(EventConstant.EventBizType.SEND_TEMPLATE);
            bizEvent.setEventBizStep(EventConstant.EventBizStep.SEND_TICKET_CONSUME_TEMPLATE);
            bizEvent.setEventData(MapConverUtil.PO2Map(eventDate));
            // logger.info(bizEvent.toString());
            String returnString = "";
            returnString = eventService.sendEvent2MQ(bizEvent);
            logger.info("推送卡券核销消息模板事件到MQ结束:" + returnString);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceBizException(e.getMessage(), e);
        }
    }

    public Map<String, Object> dealerSynchronous() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> param = new HashMap<String, Object>();
        String url = dealerUrl;
        try {
            // 调用DCMS接口
            List<DealerSynchronousDto> resultDto = HttpCommonUtil.getOwnerInfo(param, "IC009", url,
                                                                               DealerSynchronousDto.class);
            if (resultDto.size() != 0) {
                int a = tiDealerMapper.insertDealer(resultDto);
                logger.info("将所有获取到的数据成功插入了TEMP表中>>>>>>" + a);

                logger.info("更新两张表中DEALER_CODE相同的数据");
                int r = tmDealerMapper.updateSameCode();
                logger.info("更新已存在的DEALER_CODE数据量：" + r);
                List<String> codeList = tmDealerMapper.selectAdminUser();
                logger.info("更新所有在网的销售店的管理员账号为可用状态");
                Map<String, Object> query = new HashMap<String, Object>();
                String ID = "(";
                for (int o = 0; o < codeList.size(); o++) {
                    if (o == codeList.size() - 1) {
                        ID = ID + "'" + codeList.get(o) + "'" + ")";
                    } else {
                        ID = ID + "'" + codeList.get(o) + "'" + ",";
                    }
                }
                query.put("ID", ID);
                if (codeList.size() != 0) {
                    int d = tmDealerMapper.updateAdminUser(query);
                    logger.info("更新为可用状态的销售店管理员账号数量为：" + d);
                }
                logger.info("查询TM表和TI表的数据差异");
                List<TiDealer> xDto = tiDealerMapper.selectExsitByTM();
                if (xDto.size() == 0) {
                    logger.info("不存在TM表中有数据，TI表中没有数据的情况");
                } else {
                    logger.info("存在TM表中有数据，TI表中没有数据的情况，需要将TM表中的这些数据置成退网状态");
                    // String CODE = "(";
                    // Map<String, Object> map = new HashMap<String, Object>();
                    // for (int o = 0; o < xDto.size(); o++) {
                    // if(xDto.get(o).getDealerCode()!="00000"||xDto.get(o).getDealerCode()!=null){
                    // if (o == xDto.size() - 1) {
                    // CODE = CODE + "'" + xDto.get(o).getDealerCode() + "'" + ")";
                    // } else {
                    // CODE = CODE + "'" + xDto.get(o).getDealerCode() + "'" + ",";
                    // }
                    // }
                    // }
                    // map.put("CODE", CODE);
                    int b = tmDealerMapper.updateDealer();
                    logger.info("更新为退网状态的经销商数量为>>>>>" + b);
                    int e = tmDealerMapper.updateDealerUser();
                    logger.info("更新为禁用状态的经销商用户数量为>>>>>" + e);
                }

                logger.info("查询TI表和TM表的数据差异");
                List<TmDealer> sDto = tmDealerMapper.selectExsitByTI();
                if (sDto.size() == 0) {
                    logger.info("不存在TI表中有数据，TM表中没有数据的情况");
                } else {
                    logger.info("存在TI表中有数据，TM表中没有数据的情况，需要将TI表中这些数据新增到TM表中");
                    int n = 0;
                    int p = 0;
                    int q = 0;
                    List<TmUsers> userList = new ArrayList<TmUsers>();
                    String DEALER_CODE = "(";
                    String USER_CODE = "(";
                    for (int c = 0; c < sDto.size(); c++) {
                        if (c == sDto.size() - 1) {
                            DEALER_CODE = DEALER_CODE + "'" + sDto.get(c).getDealerCode() + "'" + ")";
                            USER_CODE = USER_CODE + "'" + sDto.get(c).getDealerCode() + "_ADMIN'" + ")";
                        } else {
                            DEALER_CODE = DEALER_CODE + "'" + sDto.get(c).getDealerCode() + "'" + ",";
                            USER_CODE = USER_CODE + "'" + sDto.get(c).getDealerCode() + "_ADMIN'" + ",";
                        }
                    }
                    List<TmDealer> dto = tmDealerMapper.selectDealer(DEALER_CODE);
                    logger.info("需要新增的经销商数量为：" + dto.size());
                    n = tmDealerMapper.insertTmDealer(dto);
                    logger.info("新增加的经销商数量为>>>>>" + n);
                    for (int d = 0; d < dto.size(); d++) {
                        TmDealer record = new TmDealer();
                        record = dto.get(d);
                        TmUsers users = new TmUsers();
                        users.setDealerCode(record.getDealerCode());
                        users.setLoginName(record.getDealerCode());
                        users.setUserName(record.getDealerCode());
                        userList.add(users);
                    }
                    p = tmUsersMapper.insertUsers(userList);
                    logger.info("新增加的经销商账号数量为>>>>>" + p);
                    List<String> idList = tmUsersMapper.selectUsersId(USER_CODE);
                    q = tmUsersMapper.insertRelation(idList);
                    logger.info("设置为管理员的经销商账号数量为>>>>>" + q);
                }
            } else {
                logger.info("未返回经销商数据");
            }
            resultMap.put("result", "200");
            resultMap.put("resultMsg", "同步经销商信息成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultMap.put("result", "500");
            resultMap.put("resultMsg", "同步经销商信息失败！");
        }
        return resultMap;
    }
    public Map<String, Object> dealerUsersSynchronous() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> param = new HashMap<String, Object>();
        String url = dealerUsersUrl;
        // String url = "http://yytest.imwork.net/DCMS/common/iclub/IC010/syncDealerInfo.json";
        try {
            // 调用DCMS接口
            logger.info("调用DCMS接口IC010");
            List<DealerUsersDto> resultDto = HttpCommonUtil.getOwnerInfo(param, "IC010", url, DealerUsersDto.class);
            logger.info("调用DCMS接口IC010成功");

            logger.info("将所有获取到的数据成功插入了TEMP表中开始");
            tiDealerUsersMapper.insertAllTI(resultDto);
            logger.info("将所有获取到的数据成功插入了TEMP表中");

            logger.info("删除电话为空的数据");
            tiDealerUsersMapper.deletePhoneNull();
            logger.info("删除电话为空的数据");

            /*
             * logger.info("复制relation表开始"); tiDealerUsersMapper.copyRelation(); logger.info("复制relation表结束");
             */

            logger.info("跟新TM表中数据开始");
            tiDealerUsersMapper.updateTM();
            logger.info("跟新TM表中数据成功");

            logger.info("插入TM表数据开始");
            tiDealerUsersMapper.insertTM();
            logger.info("插入TM表数据结束");

            logger.info("注销TM表数据开始");
            tiDealerUsersMapper.deleteTM();
            logger.info("注销TM表数据结束");

            logger.info("删除所有relation表开始");
            tiDealerUsersMapper.deleteAllRelation();
            logger.info("删除所有relation表结束");

            logger.info("插入relation表开始");
            tiDealerUsersMapper.insertTMRelation();
            logger.info("插入relation表结束");

            resultMap.put("result", "200");
            resultMap.put("resultMsg", "同步经销商用户信息成功！");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultMap.put("result", "500");
            resultMap.put("resultMsg", "同步经销商用户信息失败！");
        }
        return resultMap;
    }

}
